package shuixian.jing.discuz.activity;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import shuixian.jing.discuz.R;
import shuixian.jing.discuz.activity.base.FrameActivity;
import shuixian.jing.discuz.utility.MyHttpClient;
import shuixian.jing.discuz.utility.MySiteConfig;

public class ActivitySpaceMe extends FrameActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addCenterView(R.layout.space_me_body);
		
		//测试
		HttpClient httpclient = MyHttpClient.getInstance();
		 
		 // 创建一个GET请求
		 
		  HttpGet request = new HttpGet(MySiteConfig.web_rul+"api/mobile/index.php?module=profile");
		  BasicResponseHandler bHandler =   new BasicResponseHandler();
		  try {
			String response = httpclient.execute(request,bHandler);
			JSONTokener jsonParser = new JSONTokener(response); 
			  
			  JSONObject person = (JSONObject) jsonParser.nextValue();  
			  person = person.getJSONObject("Variables");
			  showLog(response);
			  //showToast(person.getString("member_username"));
			  showToast(person.getString("member_uid"));
			  //http://192.168.1.3/uc_server/avatar.php?uid=1&size=small
			
			  ImageView mImageView = (ImageView)findViewById(R.id.friend_icon);
			  
			  mImageView.setImageBitmap(getBitmapFromUrl(MySiteConfig.web_rul+"uc_server/avatar.php?uid="+person.getString("member_uid")+"&size=small"));
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		
	}
	
	
	private Bitmap getBitmapFromUrl(String imgUrl) {
        URL url;
        Bitmap bitmap = null;
        try {
                url = new URL(imgUrl);
                InputStream is = url.openConnection().getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bitmap = BitmapFactory.decodeStream(bis);
                bis.close();
        } catch (MalformedURLException e) {
                e.printStackTrace();
        } catch (IOException e) {
                e.printStackTrace();
        }
        return bitmap;
}

	
}
